package com.ruoyi.media.publishRecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.media.mediaAccount.domain.MediaAccount;
import com.ruoyi.media.mediaAccount.service.IMediaAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.media.publishRecord.domain.bo.MediaPublishRecordBo;
import com.ruoyi.media.publishRecord.domain.vo.MediaPublishRecordVo;
import com.ruoyi.media.publishRecord.domain.MediaPublishRecord;
import com.ruoyi.media.publishRecord.mapper.MediaPublishRecordMapper;
import com.ruoyi.media.publishRecord.service.IMediaPublishRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 视频号发布记录Service业务层处理
 *
 * @author dqw
 * @date 2023-05-14
 */
@RequiredArgsConstructor
@Service
public class MediaPublishRecordServiceImpl extends ServiceImpl<MediaPublishRecordMapper,MediaPublishRecord> implements IMediaPublishRecordService {

    private final MediaPublishRecordMapper baseMapper;

    /**
     * 查询账号的参数
     */
    private final IMediaAccountService mediaAccountService;

    /**
     * 查询视频号发布记录
     */
    @Override
    public MediaPublishRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询视频号发布记录列表
     */
    @Override
    public TableDataInfo<MediaPublishRecordVo> queryPageList(MediaPublishRecordBo bo, PageQuery pageQuery) {
        bo.setDelFlag(0);//查询未删除的数据
        bo.setCreateBy(LoginHelper.getLoginUser().getUsername());
        LambdaQueryWrapper<MediaPublishRecord> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(MediaPublishRecord::getCreateTime);
        Page<MediaPublishRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询视频号发布记录列表
     */
    @Override
    public List<MediaPublishRecordVo> queryList(MediaPublishRecordBo bo) {
        LambdaQueryWrapper<MediaPublishRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MediaPublishRecord> buildQueryWrapper(MediaPublishRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MediaPublishRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPublishTime() != null, MediaPublishRecord::getPublishTime, bo.getPublishTime());
        lqw.eq(bo.getPublishStatus() != null, MediaPublishRecord::getPublishStatus, bo.getPublishStatus());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), MediaPublishRecord::getNickname, bo.getNickname());
        lqw.like(bo.getDelFlag() != null, MediaPublishRecord::getDelFlag, bo.getDelFlag());
        lqw.like(StringUtils.isNotBlank(bo.getCreateBy()), MediaPublishRecord::getCreateBy, bo.getCreateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getCoverLink()), MediaPublishRecord::getCoverLink, bo.getCoverLink());
        lqw.eq(StringUtils.isNotBlank(bo.getShortTitle()), MediaPublishRecord::getShortTitle, bo.getShortTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getCityIp()), MediaPublishRecord::getCityIp, bo.getCityIp());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), MediaPublishRecord::getDescription, bo.getDescription());
        return lqw;
    }

    /**
     * 新增视频号发布记录
     */
    @Override
    public Boolean insertByBo(MediaPublishRecordBo bo) {
        MediaPublishRecord add = BeanUtil.toBean(bo, MediaPublishRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改视频号发布记录
     */
    @Override
    public Boolean updateByBo(MediaPublishRecordBo bo) {
        MediaPublishRecord update = BeanUtil.toBean(bo, MediaPublishRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MediaPublishRecord entity){
        //TODO 做一些数据校验,如唯一约束
        LoginUser loginUser =  LoginHelper.getLoginUser();
        entity.setUserId(loginUser.getUserId());
        entity.setDeptId(loginUser.getDeptId());
    }

    /**
     * 批量删除视频号发布记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    /**
     * 获取未发布账号的一条记录
     * @return
     */
    @Override
    public MediaPublishRecordVo getAccountPublishData(String username) {
        MediaPublishRecord  mediaPublishRecord =  baseMapper.getPublishData(username);

        MediaAccount mediaAccount = mediaAccountService.getById(mediaPublishRecord.getAccountId());

        MediaPublishRecordVo mediaPublishRecordVo = BeanUtil.copyProperties(mediaPublishRecord,MediaPublishRecordVo.class);
        mediaPublishRecordVo.setCookies(mediaAccount.getCookies());
        mediaPublishRecordVo.setFinderUserName(mediaAccount.getFinderUserName());
        mediaPublishRecordVo.setUniqId(mediaAccount.getUniqId());
        return mediaPublishRecordVo;
    }
}
